import pinia from "../store/store";
import piniaStore from "../store";
const store = piniaStore(pinia);
let modules = import.meta.glob("../views/**/*.vue");
import Layout from '../layout/index.vue'

const sysBaseTitle: String = '魂烧后台系统'
// 1. 配置路由
let routes: any = [
  {
    path: '/',
    component: Layout,
    children: [],
    meta: {
      title: sysBaseTitle
    }
  },{
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/login.vue'),
    meta: {
      title: '登录'
    }
  },{
    path: '/error',
    name: 'Error',
    component: () => import('../views/error/error.vue'),
    meta: {
      title: '404'
    }
  },
];

// 所有菜单列表
let allMenuList = [
  {
    id: 1,
    routerUrl: '/dashboard',
    title: '仪表盘',
    icons: 'AreaChartOutlined',
    path: '/dashboard',
    name: 'dashboard',
    component: 'views/dashboard/index.vue',
    sort: 1,
    children: [
      {
        id: 11,
        routerUrl: '/dashboard/analysis',
        title: '分析页',
        icons: 'BarChartOutlined',
        path: 'analysis',
        name: 'analysis',
        component: 'views/dashboard/analysis/analysis.vue',
        sort: 1,
      },{
        id: 12,
        routerUrl: '/dashboard/workbench',
        title: '工作台',
        icons: 'DesktopOutlined',
        path: 'workbench',
        name: 'workbench',
        component: 'views/dashboard/workbench/workbench.vue',
        sort: 1,
      }
    ]
  },{
    id: 2,
    routerUrl: '/system',
    title: '系统管理',
    icons: 'SettingOutlined',
    path: '/system',
    name: 'system',
    component: 'views/system/index.vue',
    sort: 1,
    children: [
      {
        id: 21,
        routerUrl: '/system/user',
        title: '用户列表',
        icons: 'UserOutlined',
        path: 'user',
        name: 'user',
        component: 'views/system/user/user.vue',
        sort: 1,
      },{
        id: 22,
        routerUrl: '/system/role',
        title: '角色管理',
        icons: 'UsergroupAddOutlined',
        path: 'role',
        name: 'role',
        component: 'views/system/role/role.vue',
        sort: 1,
      },{
        id: 23,
        routerUrl: '/system/menu',
        title: '菜单管理',
        icons: 'BarsOutlined',
        path: 'menu',
        name: 'menu',
        component: 'views/system/menu/menu.vue',
        sort: 1,
      }
    ]
  },{
    id: 3,
    routerUrl: '/article',
    title: '文章管理',
    icons: 'CalendarOutlined',
    path: '/article',
    name: 'article',
    component: 'views/article/index.vue',
    sort: 1,
    children: [
      {
        id: 31,
        routerUrl: '/article/list',
        title: '文章列表',
        icons: 'UserOutlined',
        path: 'list',
        name: '/article/list',
        component: 'views/article/list/list.vue',
        sort: 1,
      },{
        id: 32,
        routerUrl: '/article/menu',
        title: '文章分类',
        icons: 'UsergroupAddOutlined',
        path: 'menu',
        name: '/article/menu',
        component: 'views/article/menu/menu.vue',
        sort: 1,
      },{
        id: 33,
        routerUrl: '/article/configuration',
        title: '文章配置',
        icons: 'BarsOutlined',
        path: 'configuration',
        name: '/article/configuration',
        component: 'views/article/configuration/configuration.vue',
        sort: 1,
      }
    ]
  },
]

// 无限极菜单
// 根据返回的接口，重组菜单数据和路由数据
let menusRes: any = [];
let routesRes: any = [];

// 处理菜单和路由信息
allMenuList.forEach((item) => {
  // 菜单列表信息
  let menusResItem: any = {
    icons: item.icons,
    id: item.id,
    routerUrl: item.routerUrl,
    title: item.title,
  };
  // 路由列表信息
  let routesResItem: any = {
    path: item.path,
    name: item.name,
    meta: {
      title: item.title + "-" + sysBaseTitle,
    },
    component: modules[`../${item.component}`],
  };
  if (item.children) {
    menusResItem.children = [];
    routesResItem.children = [];
    menuRecursiveFilter(item.children, menusResItem);
    routerRecursiveFilter(item.children, routesResItem);
  }
  menusRes.push(menusResItem);
  routesRes.push(routesResItem);
})

// 菜单数据递归
function menuRecursiveFilter(children: any, menusResItem: any) {
  children.forEach((item: any) => {
    let menuObj: any = {
      icons: item.icons,
      id: item.id,
      index: item.index,
      routerUrl: item.routerUrl,
      title: item.title,
    };
    if (item.children) {
      menuObj.children = [];
      menuRecursiveFilter(item.children, menuObj);
    }
    menusResItem.children.push(menuObj);
  });
}

// 路由数据递归
function routerRecursiveFilter(children: any, routesResItem: any) {
  children.forEach((item: any) => {
    let routerObj: any = {
      path: item.path,
      name: item.name,
      meta: {
        title: item.title + "-" + sysBaseTitle,
      },
      component: modules[`../${item.component}`],
    };
    if (item.children) {
      routerObj.children = [];
      routerRecursiveFilter(item.children, routerObj);
    }
    routesResItem.children.push(routerObj);
  });
}

routes[0].children.push(...routesRes);
console.log(routes,'routesroutesroutes')
store.setMenu(menusRes);
// 导出路由router.ts
export default routes